package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjActivationSale;
import com.pmcc.cj.entity.CjCustomer;
import com.pmcc.cj.entity.CjFarm;
import com.pmcc.cj.repository.CjActivationSaleRepository;
import com.pmcc.cj.repository.CjCustomerRepository;
import com.pmcc.cj.repository.CjFarmRepository;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.ResultBean;
import com.pmcc.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tomas on 2020/10/13.
 */
@Service
@Transactional
public class CjFarmService {
    @Autowired
    CjFarmRepository cjFarmRepository;
    @Autowired
    CjCustomerRepository cjCustomerRepository;
    @Autowired
    CjActivationSaleRepository cjActivationSaleRepository;

    public ResultBean queryFarmByPage(CjFarm model, AntdPage antdPage){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=cjFarmRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public Specification<CjFarm> getSpecification(CjFarm model) {
        return new Specification<CjFarm>() {
            @Override
            public Predicate toPredicate(Root<CjFarm> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(StringUtil.isNotEmpty(model.getName())){
                    list.add(builder.like(root.get("name").as(String.class),  "%"+model.getName().trim()+"%"));
                }
                if(model.getVerifyResult() != null){
                    list.add(builder.equal(root.get("verifyResult").as(Integer.class),  model.getVerifyResult()));
                }
                query.orderBy(builder.desc(root.get("submitTime").as(Date.class)));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }
    public ResultBean verificationFarm(String id,String type){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            CjFarm farm = cjFarmRepository.findOne(id);
            if(farm != null){
                if("1".equals(type)) {
                    //审核通过
                    farm.setVerifyResult(1);
                    //验证该激活码是否有人使用，所以要查询出种地大户激活码为未使用的状态，未被删除，如果该激活码被使用了，则web端提示该激活码被使用，因为APP存在一个激活码同时俩个人填写
                    CjActivationSale activationSale = cjActivationSaleRepository.findByActivationCodeAndCodeTypeAndUseFlagAndDelFlag(farm.getCode(),1,0,0);
                    activationSale.setCodeType(1);
                    activationSale.setUseFlag(1);
                    activationSale.setUseDate(new Date());
                    //修改种地大户与激活码关联表
                    cjActivationSaleRepository.saveAndFlush(activationSale);
                    farm.setSalesmanId(activationSale.getEmployeeId());
                    farm.setSalesmanName(activationSale.getEmployeeName());
                    cjFarmRepository.saveAndFlush(farm);
                    CjCustomer customer = cjCustomerRepository.findOne(farm.getEmployeeId());
                    //修改客户的种地大户状态
                    customer.setProxy(1);
                    customer.setCardId(farm.getCardId());
                    customer.setCardName(farm.getName());
                    cjCustomerRepository.saveAndFlush(customer);
                }else {
                    //审核拒绝
                    farm.setVerifyResult(2);
                    cjFarmRepository.saveAndFlush(farm);
                }
            }
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
